package com.ysm.smfu.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ysm.smfu.R;
import com.ysm.smfu.contans.SpKey;
import com.ysm.smfu.fragment.PaysetMoneyFragment;
import com.ysm.smfu.fragment.WalletFragment;
import com.ysm.smfu.fragment.YuErFragment;
import com.ysm.smfu.utils.ToastUtils;
import com.ysm.smfu.utils.UIUtils;
import com.ysm.smfu.welcome.SharedConfig;
import com.zhy.autolayout.AutoFrameLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/10/11.
 */
public class PayActivity extends BaseActivity {
    @BindView(R.id.title_body)
    TextView titleBody;
    @BindView(R.id.mainActivity_topbar_img)
    ImageView mainActivityTopbarImg;
    @BindView(R.id.activity_top_text)
    TextView activityTopText;
    @BindView(R.id.activity_pay_container)
    AutoFrameLayout activityPayContainer;

    @BindView(R.id.extendText)
    TextView extendText;
    private int pay;
    private MyReceiver myreceiver;
    private String pay_businessid;
    private String money;
    private String jifen;
    private String remark;
    private SharedPreferences currShared;

    @Override
    protected void init() {
        super.init();
        getConfig();
    }

    /**
     * 获取基本信息
     */
    private void getConfig() {
        Intent intent = getIntent();
        pay_businessid = intent.getStringExtra(SpKey.Pay_BUSSINESSID);
        money = intent.getStringExtra(SpKey.MONEY);
        jifen = intent.getStringExtra(SpKey.JIFEN);
        remark = intent.getStringExtra(SpKey.REMARK);
//        判断收款/付款/钱包类型
        pay = intent.getIntExtra("pay", -1);
        String name = new SharedConfig(UIUtils.getContext()).getCommoneSp().getString(SpKey.NAME, "");
        currShared = new SharedConfig(UIUtils.getContext(), name).getPrivateSp();
    }

    @Override
    protected void initViews() {
        super.initViews();
        setContentView(R.layout.activity_pay);
        ButterKnife.bind(this);
        registerForFinish();

    }

    Fragment fragment;

    @Override
    protected void initData() {
        super.initData();
        switch (pay) {
            case -1:
                ToastUtils.showToast("数据异常");
                break;
            case 1:
                titleBody.setText("付款");
                fragment = new YuErFragment();
                Bundle bundle = new Bundle();
                bundle.putString(SpKey.Pay_BUSSINESSID, pay_businessid);
                bundle.putString(SpKey.MONEY, money);
                bundle.putString(SpKey.JIFEN, jifen);
                bundle.putString(SpKey.REMARK, remark);
                fragment.setArguments(bundle);
                break;
            case 2:
                titleBody.setText("收款");
                extendText.setVisibility(View.VISIBLE);
//                fragment = new ShouKuanFragment();
                fragment = new PaysetMoneyFragment();
                extendText.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
//                        账单明细
                        UIUtils.startActivity(PaySetActivity.class, "zhangdanmingxi", 0);
                    }
                });
                break;
            case 3:
                titleBody.setText("钱包");
                fragment = new WalletFragment();
                break;
        }
        mainActivityTopbarImg.setVisibility(View.GONE);
        activityTopText.setVisibility(View.GONE);
        getSupportFragmentManager().beginTransaction().replace(R.id.activity_pay_container, fragment).commit();
    }

    @Override
    protected void initListener() {
        super.initListener();
    }

    /**
     * 接收广播，关闭自己
     */
    private void registerForFinish() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("finfisPrepay");
        myreceiver = new MyReceiver();
        registerReceiver(myreceiver, intentFilter);
    }

    class MyReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            finish();
        }
    }

    @OnClick(R.id.back)
    public void onClick(View view) {
        currShared.edit().putString(SpKey.Pay_BUSSINESSID, "").commit();
        currShared.edit().putString(SpKey.JIFEN, "").commit();
        currShared.edit().putString(SpKey.MONEY, "").commit();
        currShared.edit().putString(SpKey.REMARK, "").commit();
        finish();
    }

    /**
     * 获取验证码测试
     *
     * @param
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(myreceiver);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
    }
}
